function t(t, a, e) {
    return a in t ? Object.defineProperty(t, a, {
        value: e,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : t[a] = e, t;
}

var a = require("../../utils/api.js"), e = getApp();

Page({
    data: {
        recommend: "",
        showSearchDel: !1,
        inputValue: "",
        searchHistory: [],
        showHistory: !1,
        showLoding: !0,
        showSuccesstipInfo: {
            showSuccesstip: !0,
            title: "已加入购物车",
            type: !0
        },
        carnum: "",
        specShow: !1,
        playBtn: "/images/playvideo.png",
        is_open_video: 1
    },
    onLoad: function(t) {
        var o = this;
        o.setData({
            showOfficeAccount: !0,
            default_img_small: e.globalData.default_small_img,
            default_img_big: e.globalData.default_big_img,
            is_open_video: e.globalData.is_open_goods_video
        }), wx.setNavigationBarColor({
            frontColor: wx.getStorageSync("basic").topcolor,
            backgroundColor: wx.getStorageSync("basic").topbg
        }), wx.getStorage({
            key: "searchHistory",
            success: function(t) {
                o.setData({
                    searchHistory: t.data,
                    showHistory: !0
                }), console.log(o.data.searchHistory);
            },
            fail: function(t) {},
            complete: function(t) {}
        }), a.getCartNum({
            openid: e.globalData.openid
        }).then(function(t) {
            console.log(t), wx.setStorageSync("cartnum", t.data), o.setData({
                carnum: wx.getStorageSync("cartnum")
            });
        });
    },
    ckeckWord: function(t) {
        console.log(t);
        var a = this, e = t.detail.value;
        a.setData({
            inputValue: e,
            showSearchDel: !0
        });
    },
    deltext: function() {
        this.setData({
            inputValue: "",
            showSearchDel: !1
        });
    },
    search: function() {
        var t = this, o = t.data.inputValue, n = t.data.searchHistory, s = {
            title: o,
            openid: e.globalData.openid
        };
        a.getGoods(s).then(function(a) {
            console.log(a), t.goodsorder(a), 0 == a.errno ? (t.setData({
                recommend: a.data,
                show_no: !0
            }), wx.showLoading({
                title: "查询成功",
                icon: "none",
                duration: 1e3
            })) : (t.setData({
                recommend: !1,
                show_no: !1,
                showSearchDel: !0
            }), wx.showLoading({
                title: "暂无数据",
                icon: "none",
                duration: 1e3
            }));
        }), console.log(), a.getTeamOpenid({
            openid: e.globalData.openid
        }).then(function(a) {
            t.setData({
                member: a.data.user,
                head: a.data.head_info
            });
        }), -1 == n.indexOf(o) && t.setData({
            searchHistory: n.concat(o),
            showHistory: !0
        }), wx.setStorage({
            key: "searchHistory",
            data: t.data.searchHistory,
            success: function(t) {
                console.log(t);
            },
            fail: function(t) {},
            complete: function(t) {}
        });
    },
    historysearch: function(t) {
        var o = this;
        console.log(t), o.setData({
            inputValue: t.currentTarget.dataset.title
        });
        var n = {
            title: t.currentTarget.dataset.title,
            openid: e.globalData.openid
        };
        a.getGoods(n).then(function(t) {
            console.log(t), o.goodsorder(t), 0 == t.errno ? (o.setData({
                recommend: t.data,
                show_no: !0
            }), wx.showLoading({
                title: "查询成功",
                icon: "none",
                duration: 1e3
            })) : (o.setData({
                recommend: !1,
                show_no: !1,
                showSearchDel: !0
            }), wx.showLoading({
                title: "暂无数据",
                icon: "none",
                duration: 1e3
            }));
        }), a.getTeamOpenid({
            openid: e.globalData.openid
        }).then(function(t) {
            o.setData({
                member: t.data.user,
                head: t.data.head_info
            });
        });
    },
    clearhistory: function() {
        this.setData({
            searchHistory: [],
            showHistory: !1
        }), wx.removeStorage({
            key: "searchHistory",
            success: function(t) {}
        });
    },
    add: function(o) {
        var n = this, s = n.data.recommend, i = o.currentTarget.dataset.limit, d = o.currentTarget.dataset.id, r = o.currentTarget.dataset.atid, c = {
            gid: d,
            num: 1,
            openid: wx.getStorageSync("openid"),
            at_id: r
        };
        console.log(s);
        for (var l = 0; l < s.length; l++) {
            var g = "recommend[" + l + "].isshowbtn", u = "recommend[" + l + "].curGoodsNum", p = "recommend[" + l + "].cart_id", h = n.data.recommend[l].curGoodsNum;
            if (s[l].g_id == d) {
                if (console.log(h), !(0 == i || h < i)) return void wx.showToast({
                    title: "本商品限购" + i + "件",
                    icon: "none",
                    duration: 2e3
                });
                var m;
                n.setData((m = {}, t(m, g, 2), t(m, u, parseInt(h) + 1), m)), console.log(h, n.data.recommend[l].curGoodsNum);
            }
        }
        a.addCart(c).then(function(t) {
            console.log(t);
            t.data.showbtn, t.data.count;
            var o = t.data.id, s = {
                title: n.data.inputValue,
                openid: e.globalData.openid
            };
            0 == t.errno ? (a.getGoods(s).then(function(t) {
                console.log(t), 0 == t.errno ? n.setData({
                    recommend: t.data,
                    show_no: !0
                }) : (n.setData({
                    recommend: !1,
                    show_no: !1,
                    showSearchDel: !0
                }), wx.showLoading({
                    title: "暂无数据",
                    icon: "none",
                    duration: 1e3
                }));
            }), n.successtip(), wx.setStorage({
                key: "cartnum",
                data: parseInt(t.data.count)
            }), console.log(p, o), n.setData({
                carnum: t.data.count
            }), wx.setStorageSync("carnum", t.data.count), 0 != t.data.count ? (wx.showTabBarRedDot({
                index: 2
            }), wx.setTabBarBadge({
                index: 2,
                text: t.data.count + ""
            })) : wx.hideTabBarRedDot({
                index: 2
            })) : wx.showToast({
                title: t.message,
                icon: "none",
                duration: 2e3
            });
        });
    },
    redu: function(o) {
        for (var n = this, s = n.data.recommend, i = o.currentTarget.dataset.id, d = (o.currentTarget.dataset.atid, 
        o.currentTarget.dataset.cid), r = 0; r < s.length; r++) if (s[r].g_id == i) var c = "recommend[" + r + "].isshowbtn", l = "recommend[" + r + "].curGoodsNum", g = n.data.recommend[r].curGoodsNum, r = r;
        console.log(d, g), (g = parseInt(g) - 1) > 0 ? a.savecart({
            id: d,
            num: g,
            act: "down",
            openid: e.globalData.openid
        }).then(function(a) {
            if (console.log(a), 0 == a.errno) {
                var e;
                n.setData((e = {
                    carnum: parseInt(a.data)
                }, t(e, c, 2), t(e, l, parseInt(g)), e)), console.log(n.data.carnum), wx.setStorage({
                    key: "cartnum",
                    data: parseInt(a.data)
                });
            }
        }) : a.cartGoodsDel({
            id: d,
            openid: e.globalData.openid
        }).then(function(o) {
            if (0 == o.errno) {
                var s;
                console.log(o), a.getIndexData({
                    openid: e.globalData.openid
                }).then(function(t) {
                    console.log(t.data.data.data);
                }), n.setData((s = {
                    carnum: o.data
                }, t(s, c, 1), t(s, l, 0), s)), wx.setStorageSync("cartnum", parseInt(o.data));
            }
        });
    },
    successtip: function() {
        arguments.length > 0 && void 0 !== arguments[0] && arguments[0];
        var a = this;
        a.setData(t({}, "showSuccesstipInfo.showSuccesstip", !a.data.showSuccesstipInfo.showSuccesstip)), 
        setTimeout(function() {
            a.setData(t({}, "showSuccesstipInfo.showSuccesstip", !a.data.showSuccesstipInfo.showSuccesstip));
        }, 1300);
    },
    tocart: function() {
        wx.navigateTo({
            url: "/pages/template/cart"
        });
    },
    onCloseButton: function(t) {
        this.setData({
            specShow: !this.data.specShow
        });
    },
    addSpec: function(t) {
        console.log(t);
        var o = this, n = e.globalData.openid, s = t.currentTarget.dataset.id, i = t.currentTarget.dataset.atid;
        o.setData({
            atid: i
        }), console.log(o.data.atid), a.GetSpecInfo({
            openid: n,
            id: s
        }).then(function(t) {
            if (console.log(t), 0 == t.errno) {
                var e = [], s = [];
                console.log(t.data.spec);
                for (var d in t.data.spec) {
                    var r = [], c = [];
                    s[d] = {
                        id: t.data.spec[d].id,
                        subSpec: t.data.spec[d].item[0].gsi_id
                    };
                    for (var l in t.data.spec[d].item) r[l] = t.data.spec[d].item[l].gsi_title, c[l] = t.data.spec[d].item[l].gsi_id;
                    e[d] = {
                        id: t.data.spec[d].id,
                        name: t.data.spec[d].title,
                        stem: r,
                        stem_id: c
                    };
                }
                for (var g = "", d = s.length - 1; d >= 0; d--) g += "," + s[d].subSpec;
                console.log(g, t.data.goods.g_id), a.GetSpecDeatail({
                    openid: n,
                    id_str: g,
                    gid: t.data.goods.g_id
                }).then(function(a) {
                    if (console.log(a), 0 == a.errno) {
                        var n = a.data.ggo_market_price.split("."), d = t.data.goods.g_limit_num <= 0 ? a.data.ggo_stock : parseInt(t.data.goods.g_limit_num) < parseInt(a.data.ggo_stock) ? t.data.goods.g_limit_num : a.data.ggo_stock, r = {
                            id: t.data.goods.g_id,
                            name: t.data.goods.g_name,
                            photo: t.data.goods.g_icon,
                            specInfo: a.data.ggo_title,
                            price: n,
                            oldPrice: a.data.ggo_old_price,
                            num: 1,
                            popup: e,
                            count: a.data.ggo_stock,
                            limit: d,
                            is_limit: t.data.goods.g_limit_num,
                            ggo_id: a.data.ggo_id
                        };
                        console.log(i), o.setData({
                            specDetail: r,
                            specShow: !o.data.specShow,
                            specDefault: s
                        }), onsole.log(o.data.atid);
                    } else wx.showToast({
                        title: t.message,
                        icon: "none"
                    });
                });
            } else wx.showToast({
                title: t.message,
                icon: "none"
            });
        });
    },
    popup: function(t) {
        for (var o = this, n = t.currentTarget.dataset.id, s = t.currentTarget.dataset.subid, i = o.data.specDefault, d = "", r = i.length - 1; r >= 0; r--) i[r].id == n && (i[r].subSpec = s, 
        o.setData({
            specDefault: i
        })), d += "," + i[r].subSpec;
        var c = e.globalData.openid, l = o.data.specDetail;
        a.GetSpecDeatail({
            openid: c,
            id_str: d,
            gid: l.id
        }).then(function(t) {
            if (0 == t.errno) {
                var a = t.data.ggo_market_price.split("."), e = l.is_limit <= 0 ? t.data.ggo_stock : parseInt(l.is_limit) < parseInt(t.data.ggo_stock) ? l.is_limit : t.data.ggo_stock;
                l.specInfo = t.data.ggo_title, l.price = a, l.oldPrice = t.data.ggo_old_price, l.count = t.data.ggo_stock, 
                l.limit = e, l.ggo_id = t.data.ggo_id, o.setData({
                    specDetail: l
                });
            } else wx.showToast({
                title: res.message,
                icon: "none"
            });
        });
    },
    specOnChange: function(t) {
        var a = t.detail, e = this, o = e.data.specDetail;
        o.num = a, e.setData({
            specDetail: o
        });
    },
    specAddCart: function(o) {
        var n = this, s = o.currentTarget.dataset.id, i = o.currentTarget.dataset.optionid, d = o.currentTarget.dataset.num, r = o.currentTarget.dataset.atid, c = n.data.recommend, l = o.currentTarget.dataset.limit;
        void 0 != r && "" != r || (r = n.data.atid);
        for (var g = 0; g < c.length; g++) {
            var u = "recommend[" + g + "].isshowbtn", p = "recommend[" + g + "].curGoodsNum", h = n.data.recommend[g].curGoodsNum;
            if (c[g].g_id == s) {
                if (!(0 == l || h < l)) return void wx.showToast({
                    title: "本商品限购" + l + "件",
                    icon: "none",
                    duration: 2e3
                });
                var m;
                n.setData((m = {}, t(m, u, 2), t(m, p, parseInt(h) + d), m));
            }
        }
        a.SpecAddCart({
            openid: e.globalData.openid,
            gid: s,
            ggoid: i,
            num: d,
            at_id: r
        }).then(function(t) {
            0 == t.errno ? (a.getIndexData({
                openid: e.globalData.openid
            }).then(function(t) {
                console.log(t), n.goodsorder(t), n.setData({
                    info: t.data.data.data
                });
            }), n.successtip(), 0 != t.data.count ? (wx.showTabBarRedDot({
                index: 2
            }), wx.setTabBarBadge({
                index: 2,
                text: t.data.count + ""
            })) : wx.hideTabBarRedDot({
                index: 2
            }), n.setData({
                specShow: !1,
                carnum: t.data.count
            }), wx.setStorageSync({
                key: "cartnum",
                data: parseInt(t.data.count)
            })) : wx.showToast({
                title: t.message,
                icon: "none",
                duration: 2e3
            });
        });
    },
    goodsorder: function(t) {
        for (var a = t.data, e = [], o = 0; o < a.length; o++) 0 != a[o].actual && (console.log(1), 
        e.push(a[o]));
        for (o = 0; o < a.length; o++) 0 == a[o].actual && (console.log(2), e.push(a[o]));
        a.splice(0, a.length), a.push.apply(a, e);
    },
    playvideo: function(t) {
        console.log(t);
        var a = t.currentTarget.dataset.url;
        wx.navigateTo({
            url: "/pages/goods/video?url=" + a
        });
    }
});